package aquarium.time;

import aquarium.gui.Aquarium;
import aquarium.items.AquariumItem;

import java.util.ArrayList;
import java.util.Collection;


public class Timer extends Thread {

	private TimerKind _kind;
	private ArrayList<AquariumItem> _list;
	private long _wait_time;
	private Collection<AquariumItem> _items;
	private Aquarium _aquarium;
	
	public Timer() {

	}

	public Timer(Collection<AquariumItem> items, TimerKind kind, long wait_time, Aquarium aquarium) {
		this._list = new ArrayList<AquariumItem>();
		this._wait_time = wait_time;
		this._kind = kind;
		this._items = items;
		this._aquarium = aquarium;
	}
	
	
	@Override
	public void run() {
		while(true){
			for(int i=0; i<_list.size(); i++) {
				if(!removeObserver(_list.get(i))){
					_list.get(i).update(this._kind, _items, _aquarium);
				}
			}
			
			_aquarium.modified = true;
			try {
				Thread.sleep(_wait_time);
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
		}
	}

	public boolean equals(TimerKind kind, long wait_time) {
		return _wait_time == wait_time && kind == _kind;
	}

	public void addObserver(AquariumItem aquariumItem) {
		_list.add(aquariumItem);
	}

	public boolean removeObserver(AquariumItem item){
		if(item.must_be_destroyed()){
			_list.remove(item);
			return true;
		}
		return false;
	}
}


